#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
    Module Documentation
    here
"""

# Created by  : Zhang Chengdong
# Create Date : 2025/1/11 11:51
# Version = v0.1.0

__author__ = "Zhang Chengdong"
__copyright__ = "Copyright 2025. Large scale model"
__credits__ = ['Zhang Chengdong']

__liscence__ = "MIT"
__version__ = "1.0.1"
__maintainer__ = "Zhang Chengdong"
__status__ = "Production"

import json

import numpy as np
import pandas as pd
import featuretools as ft


class FeatureTools:
    """
    对熟料，细表，熟料强度进行特征两两组合
    """

    def __init__(self, origin_data: pd.DataFrame, agg_columns: list = None):
        """
        类方法初始化
        :param origin_data:
        :param agg_columns:
        """
        self.origin_data = origin_data
        self.all_columns = self.origin_data.columns.tolist()
        if agg_columns:
            self.agg_columns = agg_columns
        else:
            self.agg_columns = ["DCS反馈配比平均值-熟料", "熟料28天强度预测", "熟料3天强度预测"]
        self.not_agg_columns = list(set(self.all_columns) - set(self.agg_columns))
        self.not_agg_columns.append("index_0")
        self.trans_primitives = ['add_numeric', 'multiply_numeric', 'divide_numeric', 'subtract_numeric']
        # self.trans_primitives = ['add_numeric', 'multiply_numeric', 'divide_numeric']
        self.agg_primitives = ['sum', 'median', 'mean']
        self.filter_data = self.origin_data.loc[:, self.agg_columns]
        self.not_filter_data = self.origin_data.loc[:, self.not_agg_columns]

    def combination_feature(self):
        """

        :return:
        """
        es = ft.EntitySet(id="feature_tools")
        es.add_dataframe(dataframe_name="cement", dataframe=self.filter_data, index="index_0")

        feature_matrix, feature_names = ft.dfs(
            entityset=es,
            target_dataframe_name="cement",
            max_depth=4,
            verbose=1,
            trans_primitives=self.trans_primitives,
            agg_primitives=self.agg_primitives,
        )
        feature_matrix = feature_matrix.reset_index()
        merge_data = pd.merge(feature_matrix, self.not_filter_data, on="index_0", how="left")
        merge_data.columns = merge_data.columns.str.replace(" ", "").str.replace("-", "_").str.replace("*", "x")
        return merge_data
